﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AQMProject
{
    static class YahooUrlGenerator
    {

        private readonly static string urlTemplate =
             @"http://ichart.finance.yahoo.com/table.csv?" +
                 "s=[symbol]&a=[startMonth]&" +
                 "b=[startDay]&" +
                 "c=[startYear]&" +
                 "d=[endMonth]&" +
                 "e=[endDay]&" +
                 "f=[endYear]&" +
                 "g=d" +
                 "&ignore=.csv";

        public static Uri createURL(string symbol, DateTime startDate, DateTime endDate)
        {
            int startMonth = startDate.Month;
            int startDay = startDate.Day;
            int startYear = startDate.Year;
            int endMonth = endDate.Month;
            int endDay = endDate.Day;
            int endYear = endDate.Year;
            return createURL(symbol, startMonth, startDay, startYear, endMonth, endDay, endYear);
        }

        public static Uri createURL(string symbol,
          int startMonth,
          int startDay,
          int startYear,
          int endMonth,
          int endDay,
          int endYear
          )
        {
            return createURL(
                symbol,
                (startMonth - 1).ToString(),
                startDay.ToString(),
                startYear.ToString(),
                (endMonth - 1).ToString(),
                endDay.ToString(),
                endYear.ToString()
                );
        }

        private static Uri createURL(string symbol,
            string startMonth,
            string startDay,
            string startYear,
            string endMonth,
            string endDay,
            string endYear
            )
        {
            String result;
            result = urlTemplate.Replace("[symbol]", symbol);

            result = result.Replace("[startMonth]", startMonth);
            result = result.Replace("[startDay]", startDay);
            result = result.Replace("[startYear]", startYear);

            result = result.Replace("[endMonth]", endMonth);
            result = result.Replace("[endDay]", endDay);
            result = result.Replace("[endYear]", endYear);

            return new Uri(result);
        }
    }

}
